package co.edu.unal.satacredito.mediation;

import interfaces.Constanst;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import model.Cluster;
import model.Host;
import model.Metrics;

@Stateless
public class MachinesUpEJB implements MachinesUpEJBLocal,MachinesUpEJBRemote,Constanst{

	@Override
	public Cluster clusterMachines() {
		String result= "";
		try {
            Socket server = new Socket("192.168.0.60", 8649);
            BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
            result = "" + Constanst.fileName + ".xml";
            File xml = new File(result);
            String entrada;
            BufferedWriter bw = new BufferedWriter(new FileWriter(xml));
            while ((entrada = br.readLine()) != null) {
                bw.write(entrada + "\n");
            }
            bw.close();
            server.close();
        } catch (IOException ioe) {
            System.out.print(ioe.toString());
            result = "Ocurrio un problema revisando el puerto o escribiendo el archivo";
        }
		Document doc = null;
        try {
            SAXBuilder domBuilder = new SAXBuilder();
            FileInputStream test = new FileInputStream("" + Constanst.fileName + ".xml");
            doc = domBuilder.build(test);
        } catch (Exception e) {
            System.out.print(e.toString());
        }
        if (doc==null){
        	return null;        	
        }
        Element raiz = doc.getRootElement();
        List<Element> clusterList = raiz.getChildren();
        ArrayList<Cluster> clusterArrayList = new ArrayList<Cluster>();
        for (Element clusterHijo : clusterList) {
            List<Element> hostList = clusterHijo.getChildren();
            ArrayList<Host> hostArrayList = new ArrayList<Host>();
            for (Element host : hostList) {
                long l = (long) Long.parseLong(host.getAttributeValue(reported));
                Host h = new Host(host.getAttributeValue(name), host.getAttributeValue(ip), host.getAttributeValue(owner), new Date(l), new ArrayList<Metrics>());
                hostArrayList.add(h);
            }
            Cluster c = new Cluster(clusterHijo.getAttributeValue(name), clusterHijo.getAttributeValue(owner), hostArrayList);
            clusterArrayList.add(c);
        }
        return clusterArrayList.get(0); 
	}

	@Override
	public ArrayList<String> ipsList() {
		Cluster cluster = clusterMachines();
		ArrayList<String> ips = new ArrayList<String>();
		if (cluster==null){
			return ips;
		}
		ArrayList<Host> hosts = cluster.getHostList();		
        for (Host h : hosts) {
            ips.add(h.getIp());
        }
        return ips;
	}
	}
